package com.tfxy.hxl.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfxy.hxl.R;
import com.tfxy.hxl.entity.Course;
import com.tfxy.hxl.entity.vo.*;
import com.tfxy.hxl.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 课程控制器
 * 使用标准的Controller → Service → Mapper架构
 */
@RestController
@RequestMapping("/course")
@Tag(name = "课程管理", description = "课程核心功能")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 1. 多条件分页查询课程
     */
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    @Operation(summary = "多条件查询课程分页")
    public R pageCourseCondition(
            @Parameter(description = "当前页码") @PathVariable Long page,
            @Parameter(description = "每页记录数") @PathVariable Long limit,
            @RequestBody(required = false) CourseQuery courseQuery) {

        // 创建分页对象
        Page<Course> pageParam = new Page<>(page, limit);

        // 调用Service层方法
        courseService.pageQuery(pageParam, courseQuery);

        List<Course> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 2. 前台课程查询（支持多种排序）
     */
    @PostMapping("/getCourseFrontInfo")
    @Operation(summary = "前台课程查询")
    public R getCourseFrontInfo(@RequestBody CourseFrontVo courseFrontVo) {

        Page<Course> pageCourse = new Page<>(1, 12); // 默认分页

        // 调用Service层方法
        Map<String, Object> result = courseService.getCourseFrontInfo(pageCourse, courseFrontVo);

        return R.ok().data("data", result);
    }

    /**
     * 3. 获取课程详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取课程详情")
    public R getCourseDetail(@Parameter(description = "课程ID") @PathVariable Long id) {
        Course course = courseService.getCourseDetail(id);
        if (course == null) {
            return R.error().message("课程不存在");
        }

        return R.ok().data("course", course);
    }

    /**
     * 4. 创建新课程
     */
    @PostMapping("/create")
    @Operation(summary = "创建课程")
    public R createCourse(@RequestBody Course course) {
        course.setCreateTime(new Date());
        course.setUpdateTime(new Date());
        course.setStatus(0); // 默认未发布
        course.setDeleted(0);
        course.setViewCount(0);
        course.setEnrollCount(0);
        course.setRatingCount(0);
        if (course.getRating() == null) {
            course.setRating(new BigDecimal("0.0"));
        }

        boolean result = courseService.save(course);
        if (result) {
            return R.ok().message("课程创建成功");
        } else {
            return R.error().message("课程创建失败");
        }
    }

    /**
     * 5. 发布课程
     */
    @PostMapping("/publishCourse/{id}")
    @Operation(summary = "发布课程")
    public R publishCourse(@Parameter(description = "课程ID") @PathVariable String id) {
        boolean result = courseService.publishCourse(Long.valueOf(id));
        if (result) {
            return R.ok().message("课程发布成功");
        } else {
            return R.error().message("课程发布失败");
        }
    }
}
